<template>
	<view class="">
		<view class="page bg-white b_y">
			<view>
				<view class="f-d  bg-white p20 a-c bb" style="padding-top: 40rpx;padding-bottom: 40rpx;display: flex;">
					<view class="  c_pl" style="width: 180rpx;">
						<text class="f28 c_z">每日活动时间:</text>
					</view>
					<lb-picker @time="time" :list="cata_time" style="flex: 1;" :level="2" mode="unlinkedSelector"
						:value="daytime" :lei="1">
						<text class="f28 c_pl" style="color:#999;">{{daytime}}</text>
					</lb-picker>
				</view>
				<view class="f-d bg-white p20 a-c bb"
					style="display: flex; padding-top: 40rpx;padding-bottom: 40rpx;display: flex;">
					<view style="width: 180rpx;" class="_pl">
						<text class="f28 c_z">选择外卖平台:</text>
					</view>
					<view class="uni-list-cell-db" style="flex: 1;">
						<radio-group @change="radioChange" class="value checked" style="font-size: 13px;">
							<label class="radio">
								<radio style="zoom: 0.8;" color="#0071FE" value="1" :checked="platform ==1" />美团
							</label>
							<label class="radio" style="margin-left:36rpx;">
								<radio style="zoom: 0.8;" color="#0071FE" value="2" :checked="platform ==2" />饿了么
							</label>
							<label class="radio" style="margin-left:36rpx;">
								<radio style="zoom: 0.8;" color="#0071FE" value="3" :checked="platform ==3" />全部
							</label>
						</radio-group>
						<!-- <lb-picker @cate="on_navcate" :list="nav_list" :level="2" mode="multiSelector" :value="nav_list"
							:lei="2">
							<text class="f28 c_pl" style="color:#999;">{{cata_name}}</text>
						</lb-picker> -->
					</view>
				</view>
				<view class="f-d  bg-white p20 a-c bb" style="padding-top: 40rpx;padding-bottom: 40rpx;">
					<view class="f28 c_pl" style="width: 180rpx;">
						<text class="f28 c_z">每单实付底限:</text>
					</view>
					<input class="f28 in" type="number" placeholder="请输入实付底限" v-model="paidin" />
				</view>
				<view class="f-d  bg-white p20 a-c bb" style="padding-top: 40rpx;padding-bottom: 40rpx;">
					<view class="f28 c_pl" style="width: 180rpx;">
						<text class="f28 c_z">每单佣金:</text>
					</view>
					<input class="f28 in" type="number" :placeholder="paidin?'佣金不能小于'+(ratio*paidin)+'元':'请输入佣金'"
						v-model="commission" />
				</view>
				<view class="f-d  bg-white p20 a-c bb" style="padding-top: 40rpx;padding-bottom: 40rpx;">
					<view class="f28 c_pl" style="width: 180rpx;">
						<text class="f28 c_z">每日份数:</text>
					</view>
					<input class="f28 in" type="number" placeholder="请输入份数" v-model="copies" />
				</view>
				<view class="f-d  bg-white p20 a-c bb" style="padding-top: 40rpx;padding-bottom: 40rpx;">
					<view class="f28 c_pl" style="width: 180rpx;">
						<text class="f28 c_z">执行天数:</text>
					</view>
					<input class="f28 in" type="number" placeholder="请输入执行天数" v-model="days" />
				</view>
				<view class="f-d  bg-white p20 a-c bb" style="padding-top: 40rpx;padding-bottom: 40rpx;display: flex;">
					<view class="f28 c_pl" style="width: 180rpx;">
						<text class="f28 c_z">执行日期:</text>
					</view>
					<picker mode="date" @change="executionDates" :start="day" style="flex: 1;color:#999;">
						<view>{{executionDate}}</view>
					</picker>
				</view>

			</view>
			<view class="flex j-c a-c">
				<button class="js-image-a" type="default"
					style="margin: 20upx;position: fixed;bottom: 120rpx;background-color: #0071FE;width: 600rpx;color: #fff;border-radius: 45rpx;"
					@tap="on_post">
					<text class="p20">创建</text>
				</button>
			</view>
		</view>
		<view class="boxs">
			<view class="">活动费用:{{(commission*copies*days)*(platform==3?2:1)}}(每单佣金*份数*天数)*平台数</view>
			<view class="">
				服务费用:{{((copies*days)*(platform==3?2:1))*publis.commissionAuditfee}}(份数*天数)*平台数*服务费
			</view>
		</view>
	</view>
</template>

<script>
	import $H from '@/common/free-lib/request.js';
	import $http from '@/common/requestConfig';
	import LbPicker from '@/common/lb-picker';
	import {
		params
	} from '@/common/time_list.js';
	export default {
		name: 'newsPublish',
		components: {
			LbPicker,
		},
		data() {
			return {
				platform: '',
				publis: '',
				nav_list: ['美团', '饿了么', '全部'],
				cata_name: '请选择平台',
				cata_time: params,
				idtushow: false,
				isYasuo: true,
				imageUrl: '',
				show: false,
				url: '',
				isdingwei: true,
				img_list: [],
				daytime: '请选择活动时间',
				day: '',
				beginTime: '', //活动开始时间		
				businessId: '', //商家标识	
				paidin: '', //实付底限
				commission: '', //佣金		
				copies: '', //份数		
				endTime: '', //活动结束时间			
				executionDate: '请选择', //执行日期	
				days: '', //天数
				ratio: '' //佣金比例
			}
		},
		onShow() {
			this.publis = uni.getStorageSync('publis')
			this.ratio = uni.getStorageSync('publis').activityPercentage || '0.5'
			// 判断是否登录
			this.$module.ifToken(res => {
				if (res == 1) {
					this.$module.navigateTo('/pages/banmi/user_login')
				} else {
					this.$module.getNowFormatDate(res => {
						this.day = res
					})
					return
				}
			})
		},
		onLoad() {},
		methods: {
			on_navcate(picker) {
				let index = picker.index[0]
				this.pickerIndex = picker.index[0]
				this.platform = picker.index[0] + 1
				this.cata_name = picker.item[0];
			},
			radioChange(e) {
				// console.log(e.detail.value)
				this.platform = e.detail.value
			},
			confirm(e) {
				console.log(e);
			},
			confirm(val) {
				this.idtushow = false;
				this.shop_lat = val.location.lat
				this.shop_lng = val.location.lng
				this.shop_addr = val.address
			},

			ok_img(url) {
				this.is_tupian = 1;
				this.shop_img1 = url.url;
				this.shop_img = '';
				this.show = false;
			},
			on_post() {
				if (!this.beginTime) {
					this.$module.showToast("请选择活动时间")
					return
				} else if (!this.paidin) {
					this.$module.showToast("请输入实付底限")
					return
				} else if (!this.commission) {
					this.$module.showToast("请输入佣金")
					return
				} else if (!this.platform) {
					this.$module.showToast("请选择平台")
					return
				} else if (this.commission < (this.paidin * this.ratio)) {
					this.$module.showToast("佣金不能小于实付" + this.ratio * 100 + '%')
					return
				} else if (!this.copies) {
					this.$module.showToast("请输入份数")
					return
				} else if (!this.days) {
					this.$module.showToast("请输选择执行天数")
					return
				} else if (!this.executionDate) {
					this.$module.showToast("请输选择执行日期")
					return
				} else {
					this.add_shop();
				}
			},
			time(picker) {
				let beginTime = picker.value[0]
				let endTime = picker.value[1]
				console.log(beginTime.replace(':', ''), endTime.replace(':', ''))
				if (parseInt(endTime) < parseInt(beginTime)) {
					this.$module.showToast('开始时间不能大于结束时间')
					this.daytime = '请选择活动时间'
				} else if (parseInt(endTime) > parseInt(beginTime)) {
					this.daytime = picker.value.join("-"); //用来显示
					this.beginTime = picker.value[0] //开始日期
					this.endTime = picker.value[1] //结束日期
				}
			},
			executionDates(e) {
				this.executionDate = e.detail.value
			},
			on_navcate(picker) {
				let index = picker.index[0]
				this.pickerIndex = picker.index[0]
				this.platform = picker.index[0] + 1
				this.cata_name = picker.item[0];
			},
			payment_front(e, id) {
				var that = this;
				let data = {
					data
				}
				let requestParams = {
					businessId: uni.getStorageSync('user').id,
					description: '用户充值',
					total: e, //e.充值金额
					orderId: id,
					platform: this.platform
				};
				this.$module.sm(requestParams, 1, res => {
					data.data = res
				})
				this.Htpp.post('/mehrb/wxpay/payment', 'app', {
					datalist: data
				}).then(ret => {
					that.$module.sm(ret, 2, res => {
						res.data.package = res.data.packageVal
						res.data.timeStamp = res.data.timestamp
						that.Payments(res.data)
					})
				})
			},
			Payments(data) {
				const that = this
				this.$module.Payment(data, res => {
					if (res == 1) {
						that.$module.showToast('充值成功')
						// let time=setTimeout(function() {
						// 	that.$module.switchTab('/pages/index/index')
						// 	clearTimeout(time)
						// }, 1600);
					}
				})
			},
			add_shop() { //入驻调用接口
				this.$module.showLoading('创建中')
				const that = this
				let date = {
					data: ''
				}
				let requestParams = {
					businessId: uni.getStorageSync('business').id, //商家id
					beginTime: this.beginTime, //活动开始时间
					commission: this.commission, //佣金		
					paidin: this.paidin, //实付底限
					copies: this.copies, //份数	
					endTime: this.endTime, //活动结束时间			
					executionDate: this.executionDate, //执行日期
					days: this.days,
					platform: this.platform
				};
				this.$module.sm(requestParams, 1, res => {
					date.data = res
				})
				this.Htpp.post('/mehrb/meplus/activity/save', 'app', {
					token: true,
					datalist: date
				}).then(ret => {
					this.$module.sm(ret, 2, res => {
						if (res.code == 0) {
							this.$module.switchTab('/pages/index/user_my')
						} else if (res.code == 60000) {
							uni.showModal({
								title: '余额不足以支付本次活动，还应充值' + res.msg + '元',
								content: '是否前去充值?',
								cancelText: "取消", // 取消按钮的文字  
								confirmText: "是", // 确认按钮的文字  
								success: function(ret) {
									if (ret.confirm) {
										that.payment_front(res.msg, res.data)
									}
								}
							});
						}
					})
				})
			},
		}
	}
</script>
<style lang="less">
	page {
		background-color: #EFEFF4;
	}

	.boxs {
		margin: 20rpx;

		view {
			color: #999;
			font-size: 30rpx;
			padding-top: 10rpx;
		}
	}

	.c_z {
		font-weight: bold;
	}

	.shuru {
		height: 250rpx;
		line-height: 50upx;
		width: 750rpx;
		border-width: 2rpx !important;
		border-right-style: solid !important;
		border-left-color: #FFFFFF !important;
		border-right-color: #FFFFFF !important;
		border-bottom-color: #D9D9D9 !important;
		border-top-color: #FFFFFF !important;
	}

	.input-view {
		font-size: 28upx;
	}

	.choose-location {
		padding: 30rpx;
	}

	.info-box {
		padding: 30rpx 0;
	}

	.info-box-item {
		padding-bottom: 20rpx;
	}

	.feedback-title {
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		padding: 20upx;
		color: #8f8f94;
		font-size: 28upx;
	}

	.c_pl {
		margin-right: 20rpx;
	}

	.in {
		width: 500rpx;
		height: 50rpx;
	}

	.close-view {
		width: 35rpx;
		height: 35rpx;
		border-radius: 50%;
		background-color: #FF5053;
		color: #FFFFFF;
		position: relative;
		top: -110px;
		right: -85px;
		font-size: 20rpx;
	}
</style>